﻿Module validaciones
    Dim dataset As New DataSet
    Public Function ValidarVacio(ByRef form As Form, ByRef errorProvider As ErrorProvider)
        Dim c As Integer
        c = 0
        For Each control In form.Controls
            If TypeOf control Is RichTextBox Then
                If control.text = "" Then
                    errorProvider.SetError(control, "Rellene este campo")
                    c = c + 1
                Else
                    errorProvider.SetError(control, "")
                End If
            End If
            If TypeOf control Is TextBox Then
                If control.text = "" Then
                    errorProvider.SetError(control, "Rellene este campo")
                    c = c + 1
                Else
                    errorProvider.SetError(control, "")
                End If
            End If
        Next
        If c = 0 Then
            Return True
        Else
            Return False
        End If

    End Function
    Public Function ValidarRut(ByVal rut_Verificar As String, ByRef control As TextBox, ByRef errorProvider As ErrorProvider) As Boolean
        If rut_Verificar <> "" Then
            Dim rutLimpio As String
            Dim digitoVerificador As String
            Dim suma As Integer
            Dim contador As Integer = 2
            Dim valida As Boolean = True
            Dim retorno As Boolean = False

            rutLimpio = rut_Verificar.Replace(".", "")
            rutLimpio = rutLimpio.Replace("-", "")
            rutLimpio = rutLimpio.Replace(" ", "")
            rutLimpio = rutLimpio.Substring(0, rutLimpio.Length - 1)
            digitoVerificador = rut_Verificar.Substring(rut_Verificar.Length - 1, 1)

            Dim i As Integer

            For i = rutLimpio.Length - 1 To 0 Step -1

                If contador > 7 Then
                    contador = 2
                End If

                Try
                    suma += Integer.Parse(rutLimpio(i).ToString()) * contador
                    contador += 1
                Catch ex As Exception
                    valida = False
                End Try

            Next

            If valida Then
                Dim dv As Integer = 11 - (suma Mod 11)
                Dim DigVer As String = dv.ToString()

                If DigVer = "10" Then
                    DigVer = "K"
                End If

                If DigVer = "11" Then
                    DigVer = "0"
                End If

                If DigVer = digitoVerificador.ToUpper Then
                    errorProvider.SetError(control, "")
                    retorno = True

                Else
                    errorProvider.SetError(control, "Rut Incorrecto")
                    retorno = False
                End If
            End If
            Return retorno
        End If
    End Function
    Public Function ValidaExistente(ByVal rut_Verificar As String, ByRef control As TextBox, ByRef errorProvider As ErrorProvider) As Boolean
        Dim retorno As Boolean = False
        Dim cant As Integer = 0
        Consulta("select count(*) as cant from usuarios where usr_rut='" & rut_Verificar & "'", dataset)
        cant = dataset.Tables("data").Rows(0)(0)
        If cant > 0 Then
            retorno = False
            ErrorProvider.SetError(Control, "El rut ya existe")
        Else
            retorno = True
        End If
        Return retorno
    End Function
    Public Function ValidaExistenteins(ByVal rut_Verificar As String, ByRef control As TextBox, ByRef errorProvider As ErrorProvider) As Boolean
        Dim retorno As Boolean = False
        Dim cant As Integer = 0
        Consulta("select count(*) as cant from investigadores where inve_rut='" & rut_Verificar & "'", dataset)
        cant = dataset.Tables("data").Rows(0)(0)
        If cant > 0 Then
            retorno = False
            errorProvider.SetError(control, "El rut ya existe")
        Else
            errorProvider.SetError(control, "")
            retorno = True
        End If
        Return retorno
    End Function
    Public Function ValidaExistenteserie(ByVal serie As String, ByRef control As TextBox, ByRef errorProvider As ErrorProvider) As Boolean
        Dim retorno As Boolean = False
        Dim cant As Integer = 0
        Consulta("select count(*) as cant from kit where kit_idex='" & serie & "'", dataset)
        cant = dataset.Tables("data").Rows(0)(0)
        If cant > 0 Then
            retorno = False
            errorProvider.SetError(control, "La serie ya existe")
        Else
            errorProvider.SetError(control, "")
            retorno = True
        End If
        Return retorno
    End Function
    Public Sub ValidaNumero(ByRef e As System.Windows.Forms.KeyPressEventArgs)
        Dim c As Char = e.KeyChar
        If Char.IsDigit(c) Then
            e.Handled = False
        ElseIf Char.IsControl(c) Then
            e.Handled = False
        Else
            e.Handled = True
        End If
    End Sub
    Public Sub ValidarCaracteresEspeciales(ByRef e As System.Windows.Forms.KeyPressEventArgs)
        Dim caracteres() As Char = {"%"c, "&"c, "$"c, "#"c, "/"c, "_"c, "'"c, "<"c, ">"c}
        Dim c As Char = e.KeyChar
        If caracteres.Contains(c) Then
            e.Handled = True
        End If
    End Sub
End Module
